
local w,h = 400,50
local w0,h0 = h,h
local w1 = w - h
local h1 = 25
local ui = class.panel:builder
{
    x = (1920-w)/2,
    y = 100,
    w = w,
    h = h,
    normal_image = [[core\bantouming.blp]],
    show_flag = false,
    name = {
        type = 'text',
        y = 5,
        h = 20,
        text = '剑宗 Lv99',
        align = 5,
        font_size = 9,
    },
    icon = {
        w = w0,
        h = h0,
        type = 'button',
        normal_image = [[UI\Console\Human\human-transport-slot.blp]],
        hover_image = [[core\hpbar\ui_b0.tga]],
        image = {
            type = 'panel',
            x = w0*0.05,
            y = h0*0.05,
            w = w0*0.9,
            h = h0*0.9,
            normal_image = get_unit_slk2('剑宗', 'Art'),
        }
    },
    hpbar = {
        x = w - w1,
        y = h/2,
        w = w1,
        h = h1,
        type = 'panel',
        normal_image = [[core\hpbar\backdrop-unit.tga]],
        fill = {
            x = 3,
            y = 3,
            w = w1 - 6,
            h = h1 - 6,
            max_w = w1 - 6,
            type = 'panel',
            normal_image = [[core\hpbar\ui_icon_05.tga]],
        },
    },
    update = function(self)
        local unit = self.unit
        if unit and unit:is_alive() and ac.clock()<self.time+3*1000 then
            local life = unit:get('生命')
            local max = unit:get('生命上限')
            self.hpbar.fill:set_width(life/max*self.hpbar.w)
            self:show()
        else
            self:hide()
        end
    end,
    set_data = function(self,unit)
        self.unit = unit
        self.time = ac.clock()
        if unit then
            local name = unit.title or unit.name
            self.name:set_text(name)
            self.icon.image:set_normal_image(unit:get_art())
            self:update()
            self:show()
        else
            self:hide()
        end
    end,
}

ac.hardware:on "界面绘制" (function ()
    if ui.show_flag and ui.handle and ui.handle~=0 then
        ui:update(hero)
    end
end)

ac.game:event '单位-造成伤害'(function(_,source,target,data)
    local player = source.owner
    if player:is_self() and data.target:is_type('Boss') and data.target:is_alive() then
        ui:set_data(data.target)
    end
end)